var request,db;
var DbName = "TestDB"; //数据库名称
var DbVersion = 1.0; //数据库版本
var TableName = "students"; //表名
var TableKey = 'rollNo';//表主键
/**
 * [一个可以进行增删改善的Indexdb快捷处理方法,by Ming°]
 * @param  {[type]}     [description]
 * @return {[type]}     [description]
 */
$(document).ready(function(){
    window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
    if(!window.indexedDB){
        console.log("你的浏览器不支持IndexedDB");
    }else{
        request = window.indexedDB.open("testDB",2);
        request.onerror = function(event){
            console.log("数据库打开失败^~^");
        }
        request.onupgradeneeded   = function(event){
            console.log("Upgrading");
            db = event.target.result;
            var objectStore = db.createObjectStore(TableName, { keyPath : TableKey });
        };
        request.onsuccess  = function(event){
            console.log("成功打开indexDB数据库^_^");
            db = event.target.result;
        }
    }
});

/**
 * [indexdb_add 添加数据]
 * @param  {[type]} rollNo [主键索引名称]
 * @param  {[type]} name   [存入的数据字符串]
 * @return {[type]}        [返回状态]
 */
function indexdb_add(key,name){
    var transaction = db.transaction([TableName],"readwrite");
    transaction.oncomplete = function(event) {
        console.log("数据添加成功^_^");
    };    
    transaction.onerror = function(event) {
        console.log("数据添加失败^~^");
    };  
    var objectStore = transaction.objectStore(TableName);
    objectStore.add({TableKey: key, name: name});
};
/**
 * [indexdb_Del 删除数据]
 * @param  {[type]} key [数据主键]
 * @return {[type]}     [description]
 */
function indexdb_del(key){                 
    db.transaction([TableName],"readwrite").objectStore(TableName).delete(key);
    console.log("主键:"+key+"数据删除成功^_^");
}
/**
 * [indexdb_find 数据查询]
 * @param  {[type]} key [主键索引]
 * @param  {[type]} show [传值则把内容插入隐]
 * @return {[type]}      [description]
 */
function indexdb_find(key,show){
    var rollNo = $("#rollno").val();
    var request = db.transaction([TableName],"readwrite").objectStore(TableName).get(key);
    request.onsuccess = function(event){
        console.log("数据查询成功^_^");
        console.info('Results:'+request.result.name);
        if (show) {
            indexdb_show(request.result.name,key);
        };
    };
}
/**
 * [indexdb_update 更新数据库操作]
 * @param  {[type]} key [主键索引]
 * @param  {[type]} name   [需要更新的值]
 * @return {[type]}        [description]
 */
function indexdb_update(key,name){
    var transaction = db.transaction([TableName],"readwrite");
    var objectStore = transaction.objectStore(TableName);
    var request = objectStore.get(key);
    request.onsuccess = function(event){
        console.info("Updating : "+request.result.name + " => " + name);
        request.result.name = name;
        objectStore.put(request.result);
        console.log("数据更新成功^_^");
    };
}
/**
 * [indexdb_del 删除数据库]
 * @param  {[type]} name [名称]
 * @return {[type]}      [description]
 */
function indexdb_delTable(name){
    indexedDB.deleteDatabase('testDB');
    console.log(name+'->数据库删除成功^_^');
}
/**
 * [indexdb_show 查询数据展示]
 * @param  {[type]} data [数据]
 * @param  {[type]} key  [主键]
 * @return {[type]}      [description]
 */
function indexdb_show(data,key){
    if ($("#indexdb").length > 0) {
        $('body').find('#indexdb').html('<input type="hidden" value="'+data+'" id="'+key+'">');
    }else{
        $('body').append('<div id="indexdb"></div>');
        $('body').find('#indexdb').html('<input type="hidden" value="'+data+'" class="'+key+'">');
    } 
}
/**
 * [sleep 程序执行sleep]
 * @param  {[type]} d [时间,毫秒]
 * @return {[type]}   [description]
 */
function sleep(d){
  for(var t = Date.now();Date.now() - t <= d;);
}